{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 17,
   "outputs": [],
   "source": [
    "import pandas as pd"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%  pandas 时间 四个概念\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "outputs": [
    {
     "data": {
      "text/plain": "0   2020-01-01\n1   2020-01-02\nName: Timestamp, dtype: datetime64[ns]"
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s1 = pd.Series([\n",
    "    '2020-01-01',\n",
    "    '2020-01-02',\n",
    "], dtype='datetime64[ns]',\n",
    "   name='Timestamp')\n",
    "s1"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% Timestamp 时间戳\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "outputs": [
    {
     "data": {
      "text/plain": "Timestamp('2020-01-01 00:00:00')"
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s1[0]"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "outputs": [
    {
     "data": {
      "text/plain": "0    2020-01-01\n1    2020-01-02\nName: Period, dtype: period[D]"
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s2 = pd.Series([\n",
    "    '2020-01-01',\n",
    "    '2020-01-02',\n",
    "], dtype='period[D]',\n",
    "   name='Period')\n",
    "s2"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% Period 时间段\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "outputs": [
    {
     "data": {
      "text/plain": "Period('2020-01-01', 'D')"
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s2[0]"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "outputs": [
    {
     "data": {
      "text/plain": "0   -1 days\n1    2 days\nName: Timedelta, dtype: timedelta64[ns]"
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s3 = pd.Series([\n",
    "    pd.Timedelta(days=-1),\n",
    "    pd.Timedelta(days=2),\n",
    "], name='Timedelta')\n",
    "s3"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% 绝对的持续时间\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "outputs": [
    {
     "data": {
      "text/plain": "Timedelta('2 days 00:00:00')"
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s3[1]"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "outputs": [
    {
     "data": {
      "text/plain": "0    <DateOffset: days=-1>\n1     <DateOffset: days=2>\nName: DateOffset, dtype: object"
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s4 = pd.Series([\n",
    "    pd.DateOffset(days=-1),\n",
    "    pd.DateOffset(days=2),\n",
    "], name='DateOffset')\n",
    "s4"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% 相对的持续时间\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "outputs": [
    {
     "data": {
      "text/plain": "0         <BusinessDay>\n1    <2 * BusinessDays>\nName: offsets_BDay, dtype: object"
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s5 = pd.Series([\n",
    "    pd.offsets.BDay(), # 周一到周五 \n",
    "    pd.offsets.BDay() * 2,\n",
    "], name='offsets_BDay')\n",
    "s5"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "outputs": [
    {
     "data": {
      "text/plain": "0   2019-12-31\n1   2020-01-04\ndtype: datetime64[ns]"
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s1 + s3"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/Yi/anaconda3/envs/pandas与办公自动化/lib/python3.7/site-packages/pandas/core/arrays/datetimelike.py:1268: PerformanceWarning: Adding/subtracting array of DateOffsets to DatetimeArray not vectorized\n",
      "  PerformanceWarning,\n"
     ]
    },
    {
     "data": {
      "text/plain": "0   2019-12-31\n1   2020-01-04\ndtype: datetime64[ns]"
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s1 + s4"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "outputs": [
    {
     "data": {
      "text/plain": "0   2020-01-02\n1   2020-01-06\ndtype: datetime64[ns]"
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s1 + s5"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "outputs": [
    {
     "data": {
      "text/plain": "   Timestamp      Period Timedelta             DateOffset        offsets_BDay\n0 2020-01-01  2020-01-01   -1 days  <DateOffset: days=-1>       <BusinessDay>\n1 2020-01-02  2020-01-02    2 days   <DateOffset: days=2>  <2 * BusinessDays>",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Timestamp</th>\n      <th>Period</th>\n      <th>Timedelta</th>\n      <th>DateOffset</th>\n      <th>offsets_BDay</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>2020-01-01</td>\n      <td>2020-01-01</td>\n      <td>-1 days</td>\n      <td>&lt;DateOffset: days=-1&gt;</td>\n      <td>&lt;BusinessDay&gt;</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>2020-01-02</td>\n      <td>2020-01-02</td>\n      <td>2 days</td>\n      <td>&lt;DateOffset: days=2&gt;</td>\n      <td>&lt;2 * BusinessDays&gt;</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.concat(\n",
    "    [s1, s2, s3, s4, s5],\n",
    "    axis=1\n",
    ")\n",
    "df"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "outputs": [],
   "source": [
    "df.to_excel('tb.xlsx', index=False)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "outputs": [
    {
     "data": {
      "text/plain": "   Timestamp      Period Timedelta             DateOffset        offsets_BDay\n0 2020-01-01  2020-01-01   -1 days  <DateOffset: days=-1>       <BusinessDay>\n1 2020-01-02  2020-01-02    2 days   <DateOffset: days=2>  <2 * BusinessDays>",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Timestamp</th>\n      <th>Period</th>\n      <th>Timedelta</th>\n      <th>DateOffset</th>\n      <th>offsets_BDay</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>2020-01-01</td>\n      <td>2020-01-01</td>\n      <td>-1 days</td>\n      <td>&lt;DateOffset: days=-1&gt;</td>\n      <td>&lt;BusinessDay&gt;</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>2020-01-02</td>\n      <td>2020-01-02</td>\n      <td>2 days</td>\n      <td>&lt;DateOffset: days=2&gt;</td>\n      <td>&lt;2 * BusinessDays&gt;</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1 = pd.read_excel(\n",
    "    'tb.xlsx',\n",
    "    dtype={\n",
    "        'Timestamp': 'datetime64[ns]',\n",
    "        'Period': 'Period[D]',\n",
    "        'Timedelta': 'timedelta64[D]'\n",
    "    }\n",
    ")\n",
    "df1"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "outputs": [
    {
     "data": {
      "text/plain": "Timestamp        datetime64[ns]\nPeriod                period[D]\nTimedelta       timedelta64[ns]\nDateOffset               object\noffsets_BDay             object\ndtype: object"
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1.dtypes"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "outputs": [
    {
     "data": {
      "text/plain": "   Timestamp      Period     Timedelta             DateOffset  \\\n0 2020-01-01  2020-01-01 -8.640000e+13  <DateOffset: days=-1>   \n1 2020-01-02  2020-01-02  1.728000e+14   <DateOffset: days=2>   \n\n         offsets_BDay  \n0       <BusinessDay>  \n1  <2 * BusinessDays>  ",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Timestamp</th>\n      <th>Period</th>\n      <th>Timedelta</th>\n      <th>DateOffset</th>\n      <th>offsets_BDay</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>2020-01-01</td>\n      <td>2020-01-01</td>\n      <td>-8.640000e+13</td>\n      <td>&lt;DateOffset: days=-1&gt;</td>\n      <td>&lt;BusinessDay&gt;</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>2020-01-02</td>\n      <td>2020-01-02</td>\n      <td>1.728000e+14</td>\n      <td>&lt;DateOffset: days=2&gt;</td>\n      <td>&lt;2 * BusinessDays&gt;</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1 = df1.convert_dtypes()\n",
    "df1"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "outputs": [
    {
     "data": {
      "text/plain": "Timestamp       datetime64[ns]\nPeriod               period[D]\nTimedelta              float64\nDateOffset              string\noffsets_BDay            string\ndtype: object"
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1.dtypes"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  },
  "pycharm": {
   "stem_cell": {
    "cell_type": "raw",
    "source": [],
    "metadata": {
     "collapsed": false
    }
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}